<?php


namespace app\app\controller\sys;

use app\common\Page;
use app\model\MenuModel;
use app\model\RoleMenuModel;
use app\model\RoleModel;
use think\facade\Db;
use app\services\Permission;

class Role
{
    /**
     * @Permission(action="sys:role:lst")
     */
    public function lst(){
        $roleModel = new RoleModel();
        $where=[['deleted','=',0]];
        if (input('role_name')){
            $where[]=['role_name','=',input('role_name')];
        }
        if (input('role_code')){
            $where[]=['role_code','=',input('role_code')];
        }
        if (input('comments')){
            $where[]=['comments','=',input('comments')];
        }
        $count = $roleModel->where($where)->count();
        $page = new Page($count,input('limit',100),input('page',1));
        $ulst = $roleModel->where($where)->limit($page->firstRow , $page->listRows)->select();
        $return['data'] = $ulst;
        $return['page']['page'] = input('page',1);
        $return['page']['limit'] = input('limit',100);
        $return['page']['count'] = $count;
        return success($return);
    }

    /**
     * @Permission(action="sys:role:add")
     */
    public function add(){
        $roleModel = new RoleModel();
        $_role = json_decode(input('role'), true);
        $_role['add_time']=time();
        $res = $roleModel::create($_role);
        return $res ? success(null,0,"添加成功") : error(1,"添加失败");
    }

    /**
     * @Permission(action="sys:role:edt")
     */
    public function edt(){
        $roleModel = new RoleModel();
        $_role = json_decode(input('role'), true);
        $_role['edit_time']=time();
        $res = $roleModel::update($_role);
        return $res ? success(null,0,"修改成功") : error(1,"修改失败");
    }

    /**
     * @Permission(action="sys:role:del")
     */
    public function del(){
        $roleModel = new RoleModel();
        $_role_id = input("role_id");
        $where[]=['role_id','in',strval($_role_id)];
        $res = $roleModel->where($where)->update(["deleted"=>1]);
        return $res ? success(null,0,"删除成功") : error(1,"删除失败");
    }

    /**
     * @Permission(action="sys:role:edt")
     */
    public function menu(){
        $menuModel = new MenuModel();
        $roleMenuModel = new RoleMenuModel();
        $where=[['role_id','=',input('role_id')]];
        $menu_lst = $menuModel->field('menu_id,parent_id,icon,title')->select();
        $role_menu_lst = $roleMenuModel->where($where)->select();
        foreach ($menu_lst as $k1=>$v1){
            foreach ($role_menu_lst as $k2=>$v2){
                if($v1['menu_id']==$v2['menu_id']){
                    $menu_lst[$k1]['checked']=true;
                }
            }
        }
        return success($menu_lst);
    }

    /**
     * @Permission(action="sys:role:edt")
     */
    public function setAuth(){
        Db::startTrans();// 启动事务
        try {
            $roleMenuModel = new RoleMenuModel();
            $roleMenuModel->where([['role_id','=',input('role_id')]])->delete();
            $ids = input('ids');
            foreach($ids as $k=>$v){
                $roleMenuModel::create([
                    'role_id'=>input('role_id'),
                    'menu_id'=>$v
                ]);
            }
            Db::commit();// 提交事务
            return success(null,0,"保存成功");
        } catch (\Exception $e) {
            Db::rollback();// 回滚事务
            return error(1,"保存失败");
        }
    }

}